package com.chapal.ib.activemq.client;

import java.io.Serializable;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Producer {
	private Destination destination;
	private int messageSize = 255;
	private long timeToLive;
	private String user = ActiveMQConnection.DEFAULT_USER;
	private String password = ActiveMQConnection.DEFAULT_PASSWORD;
	private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
	private String subject = null;
	private boolean topic;
	private boolean transacted;
	private boolean persistent = false;
	private Connection connection = null;
	MessageProducer producer = null;
	Session session = null;

	public Producer(String subject, boolean isTopic) {
		this.subject = subject;
		this.topic = isTopic;
	}

	public void init() throws Exception {
		try {

			if (this.timeToLive != 0) System.out.println("Messages time to live " + this.timeToLive + " ms");

			// Create the connection.
			final ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(this.user, this.password, this.url);
			this.connection = connectionFactory.createConnection();
			this.connection.start();

			// Create the session
			this.session = this.connection.createSession(this.transacted, Session.AUTO_ACKNOWLEDGE);
			if (this.subject == null) throw new Exception("Subject is null");
			if (this.topic) this.destination = this.session.createTopic(this.subject);
			else this.destination = this.session.createQueue(this.subject);

			this.producer = this.session.createProducer(this.destination);
			if (this.persistent) this.producer.setDeliveryMode(DeliveryMode.PERSISTENT);
			else this.producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			if (this.timeToLive != 0) this.producer.setTimeToLive(this.timeToLive);

		} catch (final Exception e) {
			System.out.println("Caught: " + e);
			e.printStackTrace();
			throw e;
		}

	}

	@Override
	protected void finalize() {
		try {
			this.connection.close();
		} catch (final Exception e) {
			e.printStackTrace();

		}
	}

	public void sendMessage(Serializable obj) throws Exception {

		if (this.session == null) throw new Exception("Session is null");
		if (this.producer == null) throw new Exception("producer is null");
		final ObjectMessage msgObject = this.session.createObjectMessage();
		msgObject.setObject(obj);
		this.producer.send(msgObject);
		if (this.transacted) this.session.commit();
	}

	public void setPersistent(boolean durable) {
		this.persistent = durable;
	}

	public void setMessageCount(int messageCount) {
	}

	public void setMessageSize(int messageSize) {
		this.messageSize = messageSize;
	}

	public void setPassword(String pwd) {
		this.password = pwd;
	}

	public void setSleepTime(long sleepTime) {
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setTimeToLive(long timeToLive) {
		this.timeToLive = timeToLive;
	}

	public void setTopic(boolean topic) {
		this.topic = topic;
	}

	public void setQueue(boolean queue) {
		this.topic = !queue;
	}

	public void setTransacted(boolean transacted) {
		this.transacted = transacted;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public void setVerbose(boolean verbose) {
	}
}
